import {
	UPLOAD_URL
} from '@/common/config.js';

//小程序转发参数
export function onShareAppMessage(parmas) {
	return {
		title: parmas ? parmas.title : '分拣员端',
		path: parmas ? parmas.path : '/pages/index/index',
		imageUrl: parmas ? parmas.imageUrl :
			'https://saas.huishendu.com/static/upload/images/3/2023/06/WechatIMG1018.png'
	}
}

export function uploadFile(obj) {
	const wx_token = uni.getStorageSync('wx_token');
	const session = uni.getStorageSync('session');
	let uploadUrl = UPLOAD_URL + `3rd_session=${session}&`
	let headers = {
		cookie: 'PHPSESSID=' + wx_token
	}
	uni.uploadFile({
		//url      : this.uploadServerUrl,
		url: uploadUrl,
		//file:this.imgFiles,
		name: "file",
		filePath: obj.url,
		//name     : this.fileName,
		formData: obj.formData || {},
		//header   : this.header,
		header: headers,
		success: (uploadRes) => {
			try {
				
				uploadRes = JSON.parse(uploadRes.data);
				if (uploadRes.result != '1') {
					uni.showToast({
						title: "上传失败 : " + uploadRes.data,
						icon: "none"
					});
				} else {
					console.log(uploadRes.data.img_url)
					obj.callback(uploadRes.data.img_url)
				}
			} catch (e) {

			}
		},
		fail: (e) => {
			uni.showToast({
				title: "上传失败，请点击图片重试",
				icon: "none"
			});
			this.error(index);

		}
	});
}

//手机号码星号显示
export function formatPhone(phoneNumber) {
	if(phoneNumber){
		return phoneNumber.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
	}else{
		return ""
	}
	
}

//解析富文本图片
export function formatRichText(richText) {
	// 主要代码
	let imgarr = [];
	let regex = new RegExp(/<img.*?(?:>|\/>)/gi); // 匹配所有图片
	let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i; // 匹配src图片
	let imageList = richText.match(regex); // obj.info 后台返回的富文本数据
	for (let a = 0; a < imageList.length; a++) {
		let srcs = imageList[a].match(srcReg);
		imgarr.push(srcs[1])
	}
	return imgarr;
}


let timer = null;
export function navigateBack(){
	timer = setTimeout(()=>{
		clearTimeout(timer)
		timer=null;
		uni.navigateBack()
	},1500)
}

export function showToastCallBack(callback){
	timer = setTimeout(()=>{
		clearTimeout(timer)
		callback()
	},1500)
}